package com.sk.cartoonindia;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.sk.cartoonindia.R;
import com.sk.utility.AsyncTaskCompleteListener;
import com.sk.utility.Constants;
import com.sk.utility.Downloader;
import com.sk.utility.Constants.FeedID;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author saurabh Activity to show categories on screen
 */
public class CategoryListActivity extends ListActivity implements
		AsyncTaskCompleteListener {
	private EditText filterText = null;
	ArrayAdapter<String> adapter = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.filterablelayout);

		filterText = (EditText) findViewById(R.id.search_box);
		filterText.addTextChangedListener(filterTextWatcher);

		filterText.setVisibility(View.INVISIBLE);
		
		GetCategories();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String item = (String) getListAdapter().getItem(position);
		Intent i = new Intent(this, ShowToonsActivity.class);
		i.putExtra("catName", item);
		startActivity(i);
	}

	private TextWatcher filterTextWatcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable arg0) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

			if (adapter != null) {
				adapter.getFilter().filter(s);
			}

		}

	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		filterText.removeTextChangedListener(filterTextWatcher);
	}

	private ArrayList<String> populateCategories(String feedStr) {

		ArrayList<String> toonPaths = new ArrayList<String>();
		try {
			int entries = 0;
			JSONArray jsonArray = new JSONArray(feedStr);
			entries = jsonArray.length();
			Log.i("GET", "Downloaded - " + entries + " categories");
			for (int i = 0; i < entries; i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				toonPaths.add(jsonObject.getString("catname"));
			}
		} catch (JSONException e) {
			Log.e("JSONException", e.getMessage());
		}
		return toonPaths;
	}

	private ProgressDialog progressDialog;

	public void GetCategories() {
		progressDialog = new ProgressDialog(this);
		progressDialog.setCancelable(true);
		progressDialog.setMessage("Loading Categories...");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setProgress(0);
		progressDialog.show();

		Downloader dn = new Downloader();
		dn.downloadJSONFeed(Constants.urlGetCategories, this,
				CategoryListActivity.this);

	}

	@Override
	public void onTaskComplete(String result, FeedID TaskID) {
		switch (TaskID) {
		case FEED:
			String ret = result;
			ArrayList<String> catsArray = new ArrayList<String>();
			catsArray = populateCategories(ret);

			if (catsArray.size() > 0) {
				String[] cats = catsArray.toArray(new String[catsArray.size()]);

				adapter = new ArrayAdapter<String>(this,
						android.R.layout.simple_list_item_1, cats);

				progressDialog.dismiss();

				filterText.setVisibility(View.VISIBLE);
				setListAdapter(adapter);

			} else {
				Toast.makeText(this, "Unable to download categories",
						Toast.LENGTH_LONG).show();
			}

			break;
		}
	}
}
